﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WidgetController.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   Widget Controller
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Controllers
{
    using System;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Web.Mvc;

    using EcBlog.Data;
    using EcBlog.Models;

    /// <summary>
    /// Widget Controller
    /// </summary>
    [Export, PartCreationPolicy(CreationPolicy.NonShared)]
    public class WidgetController : Controller
    {
        private readonly IEcUnitOfWork unitOfWork;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="WidgetController"/> class.
        /// </summary>
        /// <param name="unitOfWork">The unit of work.</param>
        [ImportingConstructor]
        public WidgetController(IEcUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        }

        /// <summary>
        /// Get the tag cloud view.
        /// </summary>
        /// <returns>The tag cloud view</returns>
        [OutputCache(Duration = 10)]
        public ActionResult TagCloud()
        {
            var totalPageCount = this.unitOfWork.PageRepository.Get().Count();
            if (totalPageCount == 0)
            {
                totalPageCount = 1;
            }

            var viewModel =
                this.unitOfWork.TagRepository.Get(c => c.Pages).Select(
                    c =>
                    new TagCloudViewModel
                        {
                            Slug = c.TagSlug, TagName = c.TagName, Weight = c.Pages.Count() * 20 / totalPageCount 
                        })
                        .ToList();
            return this.PartialView(viewModel);
        }

        /// <summary>
        /// Get the tag list.
        /// </summary>
        /// <param name="tagSlug">The tag slug.</param>
        /// <returns>The tag list.</returns>
        [OutputCache(Duration = 30)]
        public ActionResult TagList(string tagSlug = "")
        {
            var viewModel = this.unitOfWork.TagRepository.Get(c => c.Pages).ToList();
            if (!string.IsNullOrEmpty(tagSlug))
            {
                viewModel = viewModel.Where(c => c.TagSlug == tagSlug).ToList();
            }

            return this.PartialView(viewModel);
        }

        /// <summary>
        /// Get the tag list by date.
        /// </summary>
        /// <returns>The tag list</returns>
        [OutputCache(Duration = 30)]
        public ActionResult TagListByDate()
        {
            var viewModel = this.unitOfWork.TagRepository.Get(c => c.Pages).ToList();
            return this.PartialView(viewModel);
        }

        [OutputCache(Duration = 30)]
        public ActionResult BlogStatus()
        {
            this.ViewBag.pageCount = this.unitOfWork.PageRepository.Get().Count(c => c.Published < DateTime.Now && c.IsPublish);
            this.ViewBag.commentCount = this.unitOfWork.CommentRepository.Get().Count(c => c.IsPublished);
                    
            return this.PartialView();
        }
    }
}
